﻿using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;
using Eassets.Layered.Domain.Global;

namespace Eassets.Data.Global
{
    public class EassetModuleData
    {
        /// <summary>
        /// Add asset module
        /// </summary>
        /// <param name="eassetModule"></param>
        public void AddEassetModule(EassetModule eassetModule)
        {
            SqlCommand command = new SqlCommand("SP_AddEassetModule"); // Name of the SP
            CommonData commonData = new CommonData();

            commonData.ExecuteNonQuery(ConstructCommand(command, eassetModule));

        }

        /// <summary>
        /// Updates eassetModule.
        /// </summary>
        /// <param name="eassetModule"></param>
        public void ModifyEassetModule(EassetModule eassetModule)
        {
            SqlCommand command = new SqlCommand("SP_ModifyEassetModule"); // Name of the SP
            command.Parameters.AddWithValue("@EassetModuleId", eassetModule.EassetModuleId);
            CommonData commonData = new CommonData();
            commonData.ExecuteNonQuery(ConstructCommand(command, eassetModule));
        }


        /// <summary>
        /// Delete eassetModule.
        /// </summary>
        /// <param name="eassetModuleId"></param>
        public void DeleteEassetModule(long eassetModuleId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteEassetModule"); // Name of the SP
            command.Parameters.AddWithValue("@EassetModuleId", eassetModuleId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId eassetModule.
        /// </summary>
        /// <param name="eassetModuleId"></param>
        /// <returns></returns>
        public EassetModule GetByIdEassetModule(long eassetModuleId)
        {
            SqlCommand command = new SqlCommand("SP_GetEassetModuleById");
            command.Parameters.AddWithValue("@EassetModuleId", eassetModuleId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            EassetModule eassetModule = new EassetModule();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["EassetModuleId"].ToString())))
                {
                    eassetModule.EassetModuleId = Convert.ToInt32(dataReader["EassetModuleId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    eassetModule.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    eassetModule.Description = Convert.ToString(dataReader["Description"]);
                }
            }
            dataReader.Close();
            return eassetModule;
        }

        /// <summary>
        /// GetAll eassetModule.
        /// </summary>
        /// <returns></returns>
        public Collection<EassetModule> GetAllEassetModule()
        {
            Collection<EassetModule> eassetModules = new Collection<EassetModule>();
            SqlCommand command = new SqlCommand("SP_GetAllEassetModules");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            EassetModule eassetModule;
            while (dataReader.Read())
            {
                eassetModule = new EassetModule();
                if (!(String.IsNullOrEmpty(dataReader["EassetModuleId"].ToString())))
                {
                    eassetModule.EassetModuleId = Convert.ToInt32(dataReader["EassetModuleId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Code"].ToString())))
                {
                    eassetModule.Code = Convert.ToString(dataReader["Code"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    eassetModule.Description = Convert.ToString(dataReader["Description"]);
                }

                eassetModules.Add(eassetModule);
            }
            dataReader.Close();
            return eassetModules;
        }

        public SqlCommand ConstructCommand(SqlCommand command, EassetModule eassetModule)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@EassetModuleId", eassetModule.EassetModuleId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Code", eassetModule.Code);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Description", eassetModule.Description);
            }
            catch (Exception)
            { }
            return command;
        }
    }
}

